package com.og.ouroa.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.og.ouroa.common.annotations.EnableLoginHandle;
import com.og.ouroa.common.exception.Assert;
import com.og.ouroa.common.exception.BusinessException;
import com.og.ouroa.common.result.R;
import com.og.ouroa.common.result.ResponseEnum;
import com.og.ouroa.pojo.entity.Course;
import com.og.ouroa.pojo.entity.CourseStudent;
import com.og.ouroa.pojo.entity.UserInfo;
import com.og.ouroa.pojo.vo.GradeTopVO;
import com.og.ouroa.service.CourseService;
import com.og.ouroa.service.CourseStudentService;
import com.og.ouroa.service.UserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import jdk.nashorn.internal.runtime.regexp.joni.constants.CCSTATE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Us
 * @since 2021-12-13
 */
@CrossOrigin
@RestController
@RequestMapping("/admin/courseStudent")
@Api(tags = "成绩操作接口")
public class CourseStudentController {

    @Autowired
    CourseStudentService courseStudentService;

    @Autowired
    UserInfoService userInfoService;

    @Autowired
    CourseService courseService;


    @EnableLoginHandle
    @ApiOperation("查询某学生选课情况(老师权限)")
    @GetMapping("/getBySid/{page}/{limit}/{sid}")
    public R getBySid(
            @ApiParam(value = "当前页码", required = true) @PathVariable Long page,
            @ApiParam(value = "每页记录数", required = true) @PathVariable Long limit,
            @ApiParam(value = "学生id", required = true) @PathVariable Integer sid
    ) {


        IPage<Course> bySid = courseStudentService.getBySid(new Page<Course>(page, limit), sid);


        return R.ok().message("查询成功").data("课程列表", bySid);
    }

    @EnableLoginHandle(identity = UserInfo.TEACHER)
    @ApiOperation("查询某课程的选择情况(老师权限)")
    @GetMapping("/getByCid/{page}/{limit}/{cid}")
    public R getByCid(
            @ApiParam(value = "当前页码", required = true) @PathVariable Long page,
            @ApiParam(value = "每页记录数", required = true) @PathVariable Long limit,
            @ApiParam(value = "课程id", required = true) @PathVariable Integer cid
    ) {

        IPage<UserInfo> byCid = courseStudentService.getByCid(new Page<UserInfo>(page, limit), cid);


        return R.ok().message("查询成功").data("学生列表", byCid);
    }

    @EnableLoginHandle
    @ApiOperation("查询某学生某课程成绩")
    @GetMapping("/getGrade/{cid}/{sid}")
    public R getGrade(
            @ApiParam(value = "学生id", required = true) @PathVariable Integer sid,
            @ApiParam(value = "课程id", required = true) @PathVariable Integer cid
    ) {

        CourseStudent grade = courseStudentService.getGrade(sid, cid);

        Assert.notNull(grade,ResponseEnum.GRAND_NULL);
        return R.ok().message("查询成功").data("成绩", grade.getGrade());


    }

    @EnableLoginHandle(identity = UserInfo.TEACHER)
    @ApiOperation("某课程及格人数查询(老师权限)")
    @GetMapping("/countPass/{cid}")
    public R countPass(
            @ApiParam(value = "课程id", required = true) @PathVariable Integer cid

    ) {
        return R.ok().message("查询成功").data("及格人数", courseStudentService.countPass(cid));
    }

    @EnableLoginHandle(identity = UserInfo.TEACHER)
    @ApiOperation("某课程选课人数查询(老师权限)")
    @GetMapping("/countSelection/{cid}")
    public R countSelection(
            @ApiParam(value = "课程id", required = true) @PathVariable Integer cid

    ) {

        return R.ok().message("查询成功").data("选课人数", courseStudentService.countSelection(cid));
    }

    @EnableLoginHandle(identity = UserInfo.TEACHER)
    @ApiOperation("成绩排序查询(老师权限)")
    @GetMapping("/ListByCidTop/{page}/{limit}/{cid}/{type}")
    public R ListByCidTop(
            @ApiParam(value = "当前页码", required = true) @PathVariable Long page,
            @ApiParam(value = "每页记录数", required = true) @PathVariable Long limit,
            @ApiParam(value = "课程id", required = true) @PathVariable Integer cid,
            @ApiParam(value = "排序方式(1升,0降)", required = true) @PathVariable Integer type

    ) {
        if(!(type==0||type==1))
            throw new BusinessException(
                    ResponseEnum.ABNORMAL_PARA.getMessage(),
                    ResponseEnum.ABNORMAL_PARA.getCode());

        IPage<GradeTopVO> res =  courseStudentService.ListByCidTop(new Page<GradeTopVO>(page, limit), cid, type,null);



        return R.ok().message("查询成功").data("排序成绩",res);
    }

    @EnableLoginHandle(identity = UserInfo.TEACHER)
    @ApiOperation("查询成绩高于某个值的学生(老师权限)")
    @GetMapping("/ListByconditions/{page}/{limit}/{cid}/{grade}/{type}")
    public R ListByconditions(
            @ApiParam(value = "当前页码", required = true) @PathVariable Long page,
            @ApiParam(value = "每页记录数", required = true) @PathVariable Long limit,
            @ApiParam(value = "课程id", required = true) @PathVariable Integer cid,
            @ApiParam(value = "成绩范围", required = true) @PathVariable Double grade,
            @ApiParam(value = "排序方式(1升,0降)", required = true) @PathVariable Integer type
    ) {

        IPage<GradeTopVO> res =  courseStudentService.ListByCidTop(new Page<GradeTopVO>(page, limit), cid, type,grade);



        return R.ok().message("查询成功").data("排序成绩",res);

    }

    @EnableLoginHandle
    @ApiOperation("选课")
    @GetMapping("/insert/{cid}/{sid}")
    public R insert(
            @ApiParam(value = "学生id", required = true) @PathVariable Integer sid,
            @ApiParam(value = "课程id", required = true) @PathVariable Integer cid
    ) {


        Assert.notNull(userInfoService.getById(sid),ResponseEnum.LOGIN_MOBILE_ERROR);
         Assert.notNull(courseService.getById(cid),ResponseEnum.COURSE_NULL);
        courseStudentService.insert(sid,cid);


        return R.ok().message("选课成功");
    }

    @EnableLoginHandle(identity = UserInfo.TEACHER)
    @ApiOperation("成绩录入(老师权限)")
    @GetMapping("/insertGrade/{cid}/{sid}/{grade}")
    public R insert(
            @ApiParam(value = "学生id", required = true) @PathVariable Integer sid,
            @ApiParam(value = "课程id", required = true) @PathVariable Integer cid,
            @ApiParam(value = "成绩", required = true) @PathVariable Double grade
    ) {


        courseStudentService.insertGrade(sid,cid,grade);
        return R.ok().message("录入成功");
    }

    @EnableLoginHandle(identity = UserInfo.TEACHER)
    @ApiOperation("退课(老师权限)")
    @GetMapping("/delete/{cid}/{sid}")
    public R delete(
            @ApiParam(value = "学生id", required = true) @PathVariable Integer sid,
            @ApiParam(value = "课程id", required = true) @PathVariable Integer cid

    ) {

        courseStudentService.delete(sid,cid);


        return R.ok().message("退课成功");
    }


    @EnableLoginHandle
    @ApiOperation("查询某学生所有成绩")
    @GetMapping("/listBysid/{page}/{limit}/{sid}")
    public R listBysid(
            @ApiParam(value = "当前页码", required = true) @PathVariable Long page,
            @ApiParam(value = "每页记录数", required = true) @PathVariable Long limit,
            @ApiParam(value = "课程id", required = true) @PathVariable Integer sid
    ) {

        IPage<CourseStudent> bySid = courseStudentService.ListBySid(new Page<CourseStudent>(page, limit), sid);


        return R.ok().message("查询成功").data("成绩列表", bySid);
    }

}

